package com.ftz.examinationsystem.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateArrange {
    public static List<String> getIntervalTimeList(String start, String end, String am, String pm, int duration, int interval){
        Date startDate = DateUtils.convertString2Date("HH:mm:ss",start); //开始时间
        Date endDate = DateUtils.convertString2Date("HH:mm:ss",end);  //结束时间
        Date amDate=DateUtils.convertString2Date("HH:mm:ss",am); //规定的上午结束时间
        Date pmDate=DateUtils.convertString2Date("HH:mm:ss",pm);  //规定下午开始时间
        List<String> list = new ArrayList<>();

        /**
         * 开始时间-规定上午结束时间：1
         * 规定上午结束时间-规定下午开始时间：2
         * 规定下午开始时间-结束时间：3
         */

        if(startDate.getTime()>=pmDate.getTime()||endDate.getTime()<=amDate.getTime()){ //开始时间在3或结束时间在1
            Date date=startDate;
            while(startDate.getTime()<=endDate.getTime()&&date.getTime()<=endDate.getTime()){
                list.add(DateUtils.convertDate2String("HH:mm:ss",startDate));
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startDate);
                calendar.add(Calendar.MINUTE,duration);
                if(calendar.getTime().getTime()>endDate.getTime()){
                    return list;
                }else{
                    startDate=calendar.getTime();
                    list.add(DateUtils.convertDate2String("HH:mm:ss",startDate));
                    Calendar calendar1 = Calendar.getInstance();
                    calendar1.setTime(startDate);
                    calendar1.add(Calendar.MINUTE,interval);
                    startDate=calendar1.getTime();
                    Calendar calendar2 = Calendar.getInstance();
                    calendar2.setTime(startDate);
                    calendar2.add(Calendar.MINUTE,duration);
                    date = calendar2.getTime();
                }
            }
        }else if(startDate.getTime()<=amDate.getTime()&&endDate.getTime()<=pmDate.getTime()){  //开始时间在1并且结束时间在2
            Calendar calendar3=Calendar.getInstance();
            calendar3.setTime(startDate);
            calendar3.add(Calendar.MINUTE,duration);
            Date date=calendar3.getTime();
            while(startDate.getTime()<=amDate.getTime()&&date.getTime()<=amDate.getTime()){
                list.add(DateUtils.convertDate2String("HH:mm:ss",startDate));
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startDate);
                calendar.add(Calendar.MINUTE,duration);
                if(calendar.getTime().getTime()>endDate.getTime()){
                    return list;
                }else{
                    startDate=calendar.getTime();
                    list.add(DateUtils.convertDate2String("HH:mm:ss",startDate));
                    Calendar calendar1 = Calendar.getInstance();
                    calendar1.setTime(startDate);
                    calendar1.add(Calendar.MINUTE,interval);
                    startDate=calendar1.getTime();
                    Calendar calendar2 = Calendar.getInstance();
                    calendar2.setTime(startDate);
                    calendar2.add(Calendar.MINUTE,duration);
                    date = calendar2.getTime();
                }
            }
        }else if(endDate.getTime()>=pmDate.getTime()&&startDate.getTime()>=amDate.getTime()){  //开始时间在2并且结束时间在3
            Calendar calendar3=Calendar.getInstance();
            calendar3.setTime(pmDate);
//            System.out.println(pmDate);
            calendar3.add(Calendar.MINUTE,duration);
            Date date=calendar3.getTime();
//            System.out.println(date);
            while(pmDate.getTime()<=endDate.getTime()&&date.getTime()<=endDate.getTime()){
                list.add(DateUtils.convertDate2String("HH:mm:ss",pmDate));
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(pmDate);
                calendar.add(Calendar.MINUTE,duration);
                if(calendar.getTime().getTime()>endDate.getTime()){
                    return list;
                }else{
                    startDate=calendar.getTime();
                    list.add(DateUtils.convertDate2String("HH:mm:ss",startDate));
                    Calendar calendar1 = Calendar.getInstance();
                    calendar1.setTime(startDate);
                    calendar1.add(Calendar.MINUTE,interval);
                    pmDate=calendar1.getTime();
                    Calendar calendar2 = Calendar.getInstance();
                    calendar2.setTime(startDate);
                    calendar2.add(Calendar.MINUTE,duration);
                    date = calendar2.getTime();
                }
            }
        }else {  //开始时间在1并且结束时间在3
            Calendar calendar3=Calendar.getInstance();
            calendar3.setTime(startDate);
            calendar3.add(Calendar.MINUTE,duration);
            Date date=calendar3.getTime();
            while(startDate.getTime()<=amDate.getTime()&date.getTime()<=amDate.getTime()){
                list.add(DateUtils.convertDate2String("HH:mm:ss",startDate));
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startDate);
                calendar.add(Calendar.MINUTE,duration);
                if(calendar.getTime().getTime()>endDate.getTime()){
                    return list;
                }else{
                    startDate=calendar.getTime();
                    list.add(DateUtils.convertDate2String("HH:mm:ss",startDate));
                    Calendar calendar1 = Calendar.getInstance();
                    calendar1.setTime(startDate);
                    calendar1.add(Calendar.MINUTE,interval);
                    startDate=calendar1.getTime();
                    Calendar calendar2 = Calendar.getInstance();
                    calendar2.setTime(startDate);
                    calendar2.add(Calendar.MINUTE,duration);
                    date = calendar2.getTime();
                }
            }
            while(pmDate.getTime()<=endDate.getTime()&&date.getTime()<=endDate.getTime()){
                list.add(DateUtils.convertDate2String("HH:mm:ss",pmDate));
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(pmDate);
                calendar.add(Calendar.MINUTE,duration);
                if(calendar.getTime().getTime()>endDate.getTime()){
                    return list;
                }else{
                    startDate=calendar.getTime();
                    list.add(DateUtils.convertDate2String("HH:mm:ss",startDate));
                    Calendar calendar1 = Calendar.getInstance();
                    calendar1.setTime(startDate);
                    calendar1.add(Calendar.MINUTE,interval);
                    pmDate=calendar1.getTime();
                    Calendar calendar2 = Calendar.getInstance();
                    calendar2.setTime(startDate);
                    calendar2.add(Calendar.MINUTE,duration);
                    date = calendar2.getTime();
                }
            }
        }
        return list;
    }

    public static int jiaoji(Date rightS,Date rightE,Date leftS,Date leftE){
        if(
                ((leftS.getTime()<=rightS.getTime())&&leftE.getTime()<rightE.getTime()&&leftE.getTime()>rightS.getTime())
                        ||((leftS.getTime()>rightS.getTime())&&leftS.getTime()<rightE.getTime()&&leftE.getTime()>=rightE.getTime())
                        ||(leftS.getTime()<rightS.getTime()&&leftE.getTime()==rightE.getTime())
                        ||(leftS.getTime()==rightS.getTime()&&leftE.getTime()>rightE.getTime())
                        ||(leftS.getTime()>rightS.getTime()&&leftE.getTime()<rightE.getTime())
                        ||(leftS.getTime()<rightS.getTime()&&leftE.getTime()>rightE.getTime())
        ){
            return 1;
        }else {
           return 0;
        }
    }
}
